//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        menuArray: null,
        userInfo: null,
        userAge: null,
        navHeight: app.globalData.statusBarHeight * 2 + 86,
        navbarData: {
            title: '个人中心'
        },
        nowUserId: app.globalData.userId,
        message: null,
        code: 0
    },
    onLoad() {

      
    },
    onShow() {

      
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: app.globalData.tabIndex
            })
          
        }
        var that = this;
        let {openid,accountId} = wx.getStorageSync('logindata')
        wx.request({
            // url: app.getUrl('/xcx/pt/getCurrentUserInfo'),
            url: app.getUrl(`/xcx/pt/getCurrentUserInfo?openId=${openid}&accountId=${accountId}`),
            method: 'GET',
            header: wx.getStorageSync('cookie'),
            success(res) {
                if (res.data.code == 0) {
                    that.calcuAge(res.data.data.birthday);
                    that.setData({
                        userInfo: res.data.data,
                        code: res.data.code
                    })
                } else {
                    that.setData({
                        code: res.data.code,
                        message: res.data.message,
                        userInfo: null,
                        userAge: null
                    })
                }
            }
        })
        wx.request({
            url: app.getUrl('/xcx/pt/getQueueMenu'),
            method: 'GET',
            header: wx.getStorageSync('cookie'),
            data: {
                type: 2
            },
            success(res) {
                if (res.data.code == 0) {
                    that.setData({
                        menuArray: res.data.data
                    })
                } else {
                    that.setData({
                        menuArray: null
                    })
                }
            }
        })
    },
    //菜单点击
    bindMenuTap: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },

    // 功能设置
    toFunctionSetting: function () {
        wx.navigateTo({
            url: '../setting/functionSetting/functionSetting',
        })
    },


    // 关于我们
    toAboutUs: function () {
        wx.navigateTo({
            url: '../setting/aboutUs/aboutUs',
        })
    },

    toPersonInfo: function () {
        console.log('data',this.data)
        console.log('app.globalData info',app.globalData)
        if (this.data.code == 0) {
            let type = app.globalData.hospitalInfo?.remark1
            // if(this.data.userInfo.tenantId=='beishengxsekta001'||
            //  this.data.userInfo.tenantId=='cdsfnetzxyyta001' ||
            //  this.data.userInfo.tenantId=='szssjkxyjyta001' ||
            //  this.data.userInfo.tenantId=='zibzdbzsjkta001' 
            //  ){
                if(type==1){
                wx.navigateTo({
                    url: `../advertise/advertise?url=/H5/view-html/scale/html/index.html#/newArchive&userId=${this.data.userInfo.id}&queueId=${app.globalData.queueId}&templId=${this.data.userInfo.templId}&exist=1`
                })
            }else{
               wx.navigateTo({
                url: '../setting/Info/Info'
               })
            }
           
        } else if (this.data.code == 403) {
            wx.navigateTo({
                url: '../login/login',
            })
        } else if (this.data.code == 404) {
            wx.request({
                url: app.getUrl('/xcx/pt/getUsersInfo'),
                method: 'GET',
                header: wx.getStorageSync('cookie'),
                success(res) {
                    if (res.data.code == 0) {
                        wx.navigateTo({
                            url: '../guide/user_list/userList',
                        })
                    } else if (res.data.code == 200) {
                        wx.navigateTo({
                            url: "../guide/queue_list/queueList?ptUserId=" + res.data.data[0].id,
                        })
                    } else if (res.data.code == 404) {
                        wx.navigateTo({
                            url: '../guide/queue/queue?code=true',
                        })
                    }
                }
            })
        } else if (this.data.code == 405) {
            wx.navigateTo({
                url: '../guide/queue_list/queueList',
            })
        }
    },

    // 意见反馈
    toFeedback: function () {
        wx.navigateTo({
            url: '../setting/feedback/feedback',
        })
    },


    // 年龄计算
    calcuAge: function (birthDay) {
        var nowDate = new Date();
        var birthDayDate = new Date(birthDay);
        var age = "";
        // if (nowDate.getFullYear() - birthDayDate.getFullYear()>0){
        //   if ((nowDate.getMonth() - birthDayDate.getMonth()>=0)){
        //     age = (nowDate.getFullYear() - birthDayDate.getFullYear()) + "岁" + (nowDate.getMonth() - birthDayDate.getMonth())+"月";
        //   }else{
        //     if ((nowDate.getFullYear() - birthDayDate.getFullYear() - 1) == 0) {
        //       age = (nowDate.getMonth() + 12 - birthDayDate.getMonth()) + "月龄";
        //     } else {
        //       age = (nowDate.getFullYear() - birthDayDate.getFullYear() - 1) + "岁" + (nowDate.getMonth() + 12 - birthDayDate.getMonth()) + "月";
        //     }
        //   }
        // } else if (nowDate.getMonth() - birthDayDate.getMonth() >= 0) {
        //   age = (nowDate.getMonth() - birthDayDate.getMonth()) + "月龄";
        // }
        var day = nowDate.getDate() - birthDayDate.getDate()
        var month = nowDate.getMonth() - birthDayDate.getMonth()
        var year = nowDate.getFullYear() - birthDayDate.getFullYear()
        if (day < 0) {
            month = month - 1
            //判断月份天数
            var nowMonth = nowDate.getMonth() - 1
            if (nowMonth == 1 || nowMonth == 3 || nowMonth == 5 || nowMonth == 7 || nowMonth == 8 || nowMonth == 10 || nowMonth == 12) {
                day = nowDate.getDate() - birthDayDate.getDate() + 31
            } else if (nowMonth == 4 || nowMonth == 6 || nowMonth || nowMonth == 9 || nowMonth == 11) {
                day = nowDate.getDate() - birthDayDate.getDate() + 30
            } else {
                if ((nowDate.getFullYear() % 4 == 0 && nowDate.getFullYear() % 100 != 0) || (nowDate.getFullYear() % 400 == 0)) {
                    day = nowDate.getDate() - birthDayDate.getDate() + 29
                } else {
                    day = nowDate.getDate() - birthDayDate.getDate() + 28
                }
            }
        }
        if (month < 0) {
            month = month + 12
            year = year - 1
        }
        if (year > 0) {
            age = year + "岁"
        }
        if (month > 0) {
            age = age + month + "月"
        }
        if (day > 0) {
            age = age + day + "天"
        }
        this.setData({
            userAge: age
        })
    },

})